package com.mimc.controller.admin;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Page;
import com.mimc.common.utils.StringUtils;
import com.mimc.core.model.*;
import com.mimc.core.service.SysAdminService;
import com.mimc.core.service.SysMenuService;
import com.mimc.core.service.SysRoleMenuService;
import com.mimc.core.service.SysUserRoleService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.framework.jwt.AdminTokenUtils;
import com.mimc.service.ShSysMenuService;
import io.jboot.Jboot;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/admin/sys/admin")
public class SysAdminController extends JbootController {
    @Inject
    private SysAdminService sysAdminService;

    @Inject
    private SysMenuService sysMenuService;

    @Inject
    private ShSysMenuService shSysMenuService;
    @Inject
    private SysUserRoleService sysUserRoleService;
    @Inject
    private SysRoleMenuService sysRoleMenuService;
    public AjaxResult getInfo(){
        SysAdmin sysAdmin= AdminTokenUtils.getCurrentUser();
        sysAdmin.setPassword("");
        return AjaxResult.success(sysAdmin);
    }

    public AjaxResult getRoleList(){
        SysAdmin sysAdmin=AdminTokenUtils.getCurrentUser();
        if(sysAdmin.getId()==1){
            //超级管理员
            List<SysMenu> menuList=sysMenuService.findListByColumns(Columns.create(),"sort asc");
            List<SysMenu> menutree=shSysMenuService.buildMenuTree(menuList);
            return AjaxResult.success(menutree);
        }else {
            //获取角色
            List<SysUserRole> userRoles=sysUserRoleService.findListByColumns(Columns.create("admin_id",sysAdmin.getId()));
            List<Integer> roleIds=new ArrayList<>();
            for (int i=0;i<userRoles.size();i++){
                roleIds.add(userRoles.get(i).getRoleId());
            }
            List<SysRoleMenu> roleMenus=sysRoleMenuService.findListByColumns(Columns.create().in("role_id",roleIds));
            List<Long> menuIds=new ArrayList<>();
            for (int i=0;i<roleMenus.size();i++){
                menuIds.add(roleMenus.get(i).getMenuId());
            }
            List<SysMenu> sysMenus=sysMenuService.findListByColumns(Columns.create().in("menu_id",menuIds),"sort asc");
            List<SysMenu> menutree=shSysMenuService.buildMenuTree(sysMenus);
            return AjaxResult.success(menutree);
        }

    }
    public AjaxResult getList(){
        Integer page=getInt("page",1);
        Integer pageSize=getInt("pageSize",15);
        Page<SysAdmin> list=sysAdminService.paginate(page,pageSize);
        return AjaxResult.success("成功",list);
    }

    public AjaxResult add(@Para("") SysAdmin sysAdmin){
        sysAdmin.setCreateTime(new Date());
        String md5Key= Jboot.configValue("yminfo.md5key");
        String md5Pwd= SecureUtil.md5(sysAdmin.getPassword()+ md5Key);
        sysAdmin.setPassword(md5Pwd);
        Integer adminId= (Integer) sysAdminService.save(sysAdmin);
        String roleId=getPara("roleId");
        String[] roleIds=roleId.split(",");
        for (int i=0;i<roleIds.length;i++){
            SysUserRole sysUserRole=new SysUserRole();
            long id = IdUtil.getSnowflakeNextId();
            sysUserRole.setId(id);
            sysUserRole.setRoleId(Integer.parseInt(roleIds[i]));
            sysUserRole.setAdminId(adminId);
            sysUserRole.setCreateTime(new Date());
            sysUserRole.setUpdateTime(new Date());
            sysUserRoleService.save(sysUserRole);
        }
        return AjaxResult.success("成功");
    }
    public AjaxResult update(@Para("") SysAdmin sysAdmin){
        //移除关联
        List<SysUserRole> userRoles=sysUserRoleService.findListByColumns(Columns.create("admin_id",sysAdmin.getId()));
        for (int i=0;i<userRoles.size();i++){
            sysUserRoleService.delete(userRoles.get(i));
        }
        if(StringUtils.isNotBlank(sysAdmin.getPassword())){
            String md5Key= Jboot.configValue("yminfo.md5key");
            String md5Pwd= SecureUtil.md5(sysAdmin.getPassword()+ md5Key);
            sysAdmin.setPassword(md5Pwd);
        }else{
            SysAdmin oldAdmin=sysAdminService.findById(sysAdmin.getId());
            sysAdmin.setPassword(oldAdmin.getPassword());
        }
        sysAdmin.setUpdateTime(new Date());
        sysAdminService.update(sysAdmin);
        String roleId=getPara("roleId");
        String[] roleIds=roleId.split(",");
        for (int i=0;i<roleIds.length;i++){
            SysUserRole sysUserRole=new SysUserRole();
            long id = IdUtil.getSnowflakeNextId();
            sysUserRole.setId(id);
            sysUserRole.setRoleId(Integer.parseInt(roleIds[i]));
            sysUserRole.setAdminId(sysAdmin.getId());
            sysUserRole.setCreateTime(new Date());
            sysUserRole.setUpdateTime(new Date());
            sysUserRoleService.save(sysUserRole);
        }
        return AjaxResult.success("成功");
    }
    public AjaxResult delete(Integer adminId){
        SysAdmin sysAdmin=sysAdminService.findById(adminId);
        sysAdminService.delete(sysAdmin);
        //删除关联角色
        List<SysUserRole> userRoles=sysUserRoleService.findListByColumns(Columns.create("admin_id",sysAdmin.getId()));
        for (int i=0;i<userRoles.size();i++){
            sysUserRoleService.delete(userRoles.get(i));
        }
        return AjaxResult.success("成功");
    }
}
